
var player = new Player();
var boxes = new Array();
var camera = new Camera();
 debug = false;




function animate()
{



 requestAnimationFrame(function() {
  	animate();
  	delta = time; //use delta to cache the time so we don't need another var;
    time =  (Date.now() / 1000) - startTime;
    delta = time - delta; //get the difference, or time between frames.
    // console.log(currentState);
	if(!currentState.initialized)
	{
		currentState.init();
	}
	else{currentState.update();}

    });
}

boxes.update = function()
{
	// console.log(boxes);
	//"if needs update"
	var link;
	for (var i = boxes.length - 1; i >= 0; i--) {
		if(boxes[i].needsUpdate)
		{  
			 
			boxes[i].rotation -= boxes[i].rotation/2;
			if(Math.abs(boxes[i].rotation) < 0.3)
			{
				boxes[i].rotation = 0;
				boxes[i].needsUpdate = false; 
			}

			// console.log("update " , boxes[i].rotation);
			boxes[i].element.parentNode.style.webkitTransform = "rotate("+ boxes[i].rotation + "deg)";
		}
	};
	
	// console.log(boxes);
} 


function checkBounds()
{
    var elements = document.getElementsByClassName("box");
    var videos = document.getElementsByClassName("video");

	boxes.length = 0;
	var bottom = new Object();
	// bottom.rotation = 0;
	// bottom.left = -1000;
	// bottom.top = canvasBuffer.height +3;
	// bottom.right = 100000;
	// bottom.bottom = canvasBuffer.height; 
	boxes.push(game.floor);
    for(var i = 0; i<elements.length; ++i)
    {
    	console.log(elements.length);
    	var box = new Box(elements[i], camera);
    	if(!hasClass(elements[i], "powerup")){
    	elements[i].onclick = function(e){
    		 
    		 
    		
     		if(hasClass(this, "powerup"))
     		{
     		//	var child = document.getElementById(this.getAttribute("data-powerup-id"));
     		//	triggerPowerup(child);	
			// 	child.onclick = function(e){
			// 	hitLink(this.href, null, this.target);
			// }
     		}
    	 	else{hitLink(this.href, null, this.target);
    	 	e.preventDefault();}
		}
	}
        boxes.push(box);

    }
    var powerups = document.getElementsByClassName("powerup");
    // for(var i = 0; i<powerups.length; ++i)
    // {
    // 	powerups[i].firstChild
    // }
    
    // for(var i = 0; i<videos.length; ++i)
    // {
    // 	var video = new Video(videos[i], camera);
    	
    //     boxes.push(video);

    // }
 		 

	// then do the same for platforms, etc
 
}
checkBounds();

 
function collides() 
{
	var collision;
	var x1 = player.x;
	var y1 = player.y;
	var w1 = player.width;
	var h1 = player.height;

	var collided = false;
	for (var i = boxes.length - 1; i >= 0; i--) 
	{

		x1 = player.x ;
		y1 = player.y ;
		w1 = player.width;
		h1 = player.height;

		var x2 = boxes[i].left;
	    var y2 = boxes[i].top;
	    var w2 = boxes[i].width;
	    var h2 = boxes[i].height;
	    w2 += x2;
	    w1 += x1;
	    if (x2 > w1) 
    	{ 
    		continue; 
    	}
	    if ( x1 > w2) 
    	{ 
    		continue; 
    	}

	    h2 += y2;
	    h1 += y1;

	    if (y2 > h1)
	    { 
    		continue; 
    	} 
    	if( y1 > h2) 
    	{ 
    		continue; 
    	}
    	collision = boxes[i];

    	return collision;
	 
	     
	};

    return false;
    
}






animate();

 